package org.dromara.common.mongo;


import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SessionMongoDao {

    @Autowired
    private MongoOperations mongoOperations;

    private static String collectionName = "session";


    public List<Session> listSession(Long userId, Long agentId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        if (agentId != null) {
            query.addCriteria(Criteria.where("smartAgentId").is(agentId));
        }
        Sort sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime"));
        query.with(sort);
        return mongoOperations.find(query, Session.class, collectionName);
    }

    public List<Session> findSession(Long userId, String sessionId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.addCriteria(Criteria.where("sessionId").is(sessionId));
        return mongoOperations.find(query, Session.class, collectionName);
    }



    public void deleteSession(String sessionId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("sessionId").is(sessionId));
        mongoOperations.remove(query, Session.class, collectionName);
    }


    public Session findSession(String sessionId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("sessionId").is(sessionId));
        List<Session> sessions = mongoOperations.find(query, Session.class, collectionName);
        return CollectionUtil.isEmpty(sessions) ? null : sessions.get(0);
    }


    public void saveOrUpdate(Long userId, String content, String sessionId, Long agentId, String agentName, String sessionName) {
        List<Session> sessions = findSession(userId, sessionId);
        if (CollectionUtils.isEmpty(sessions)) {
            Session session = Session.builder()
                    .sessionId(sessionId)
                    .userId(userId)
                    .content(content)
                    .smartAgentId(agentId)
                    .smartAgentName(agentName)
                    .sessionName(sessionName)
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();

            mongoOperations.insert(session, collectionName);
        } else {
            Query query = new Query();
            query.addCriteria(Criteria.where("userId").is(userId));
            query.addCriteria(Criteria.where("sessionId").is(sessionId));

            Update update = new Update();
            update.set("content", content);
            update.set("updateTime", LocalDateTime.now());

            mongoOperations.updateMulti(query, update, collectionName);
        }
    }

}
